package com.enjoyor.soa.traffic.server.efoms.dubbo.listener;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.enjoyor.soa.traffic.core.efoms.domain.DeviceDetailInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.DeviceStatusInfo;
import com.enjoyor.soa.traffic.core.efoms.enums.EnumDicPublic;
import com.enjoyor.soa.traffic.core.efoms.service.IDevStatusInfoService;
import com.enjoyor.soa.traffic.core.efoms.service.IDeviceDetailInfoService;
import com.enjoyor.soa.traffic.server.efoms.common.Global;
import com.enjoyor.soa.traffic.server.efoms.helper.FTPAllFilesHelper;
import com.enjoyor.soa.traffic.server.efoms.helper.InputStreamToStringHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.helper.XmlHelper;

@Component
public class ScheduleVideoPicStatus {

	@Autowired
    private IDevStatusInfoService devStatusInfoService;
    @Autowired
    private IDeviceDetailInfoService deviceDetailInfoService;
	//默认操作人
		String personName = "获取视频图片状态";

	    @Value("${bayonet.pic.issyn}")
	    private String bayonetPicIssyn;
	    
	    @Value("${video.cap.issyn}")
	    private String videoCapIssyn;
	    @Value("${ftp.upload.ip}")
	    private String ftpIp;
		@Value("${ftp.upload.port}")
	    private String ftpPort;
		@Value("${ftp.upload.user}")
	    private String ftpUser;
		@Value("${ftp.upload.password}")
	    private String ftpPassword;
		
		//定时获取视频图片质量,每天中午十二点执行；
		@Scheduled(cron="0 0 12 * * ?")
	    public void getVideoPicStatus() throws Exception{
	    	if(!"1".equals(videoCapIssyn)) return;
	    	FTPAllFilesHelper ftpFilesHelper = new FTPAllFilesHelper(true);
	        String dirName = TimeHelper.dateToString(TimeHelper.addDate(new Date(), -1), "yyyyMMdd");
	        try {
	        		String ftpPath = "/"+dirName;
	        		String fileName = dirName+".xml";
	        		String stringInputStream = InputStreamToStringHelper.getStringStream(ftpIp, ftpUser, ftpPassword, Integer.parseInt(ftpPort), ftpPath, fileName);
	            	org.dom4j.Element ele = XmlHelper.getRootElement(stringInputStream);
	    			List<Element> list = XmlHelper.getSpecificElement(ele,"VideoNode");
	    			for(int i = 0;i<list.size();i++){
	    				List<Element> listSignal = XmlHelper.getSpecificElement(list.get(i),"Signal");
	    				List<Element> listDefinition = XmlHelper.getSpecificElement(list.get(i),"Definition");
	    				List<Element> listBrightness = XmlHelper.getSpecificElement(list.get(i),"Brightness");
	    				List<Element> listColorCast = XmlHelper.getSpecificElement(list.get(i),"ColorCast");
	    				String videoId = list.get(i).attributeValue("Id");
	    				String Definition = listSignal.get(0).attributeValue("Flag");
	    				String Signal = listDefinition.get(0).attributeValue("Flag");
	    				String Brightness = listBrightness.get(0).attributeValue("Flag");
	    				String ColorCast = listColorCast.get(0).attributeValue("Flag");
	    				boolean picStatusCode = true;
	    				if(Definition.equals("true")&&Definition.equals("true")&&Definition.equals("true")&&Definition.equals("true")){
	    					picStatusCode = true;
	    				}else{
	    					picStatusCode = false;
	    				}
	    				String typeCode = EnumDicPublic.VIDEO.getKey();
	    				List<DeviceDetailInfo> videoInfoList = deviceDetailInfoService.selectDeviceDetailInfoList(
	    						typeCode, null, null, null, null, null, null);
	    						for(int j = 0;j<videoInfoList.size();j++){
	    							if(videoId.equals(videoInfoList.get(j).getVideoPlayId())){
	    								String key = typeCode + "-" + videoInfoList.get(j).getDevId();
	    								DeviceStatusInfo deviceStatusPool = Global.DevStatusPool.get(key);
	    								//更新设备状态表以及对应内存
	    								if(null == deviceStatusPool) { 
	    									DeviceStatusInfo deviceStatusInfo = new DeviceStatusInfo();
				    	        			deviceStatusInfo.setDevCategoryCode(EnumDicPublic.DEV_CATEGORY.getKey());
				    	        			deviceStatusInfo.setDevCategoryName(EnumDicPublic.DEV_CATEGORY.getValue());
				    	        			deviceStatusInfo.setDevTypeCode(EnumDicPublic.VIDEO.getKey());
				    	        			deviceStatusInfo.setDevTypeName(EnumDicPublic.VIDEO.getValue());
				    	        			deviceStatusInfo.setDevId(videoInfoList.get(j).getDevId());
				    	        			deviceStatusInfo.setDevName(videoInfoList.get(j).getDevName());
				    	        			deviceStatusInfo.setPicStatusFlag(picStatusCode);
				    	        			devStatusInfoService.insertDevStatusInfo(deviceStatusInfo, personName);
	    									Global.DevStatusPool.put(key, deviceStatusInfo);
	    								}else if(picStatusCode != deviceStatusPool.getPicStatusFlag()) {
	    									deviceStatusPool.setPicStatusFlag(picStatusCode);
	    									devStatusInfoService.updateDevStatusInfo(deviceStatusPool, personName);
	    									Global.DevStatusPool.put(key, deviceStatusPool);
	    								}
	    							}
	    						}		
	    			}
				ftpFilesHelper.disConnection();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		public static void main(){
			ScheduleVideoPicStatus sta = new ScheduleVideoPicStatus();
			try {
				sta.getVideoPicStatus();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
}
